#include <stdio.h>
#include <string.h>
#include "base64.h"
#define MAX_MSG_LENGTH 8192

struct hardinfo {
    char uid[10];
    char key[10];
} Infolist[] = {
    {"36497784", "299097126"},
    {"48396036", "627645753"},
    {"37172568", "62808925"},
    {"63090107", "16807700"},
    {"26931022", "3973736"}
};

const int Infolistsize = sizeof(Infolist)/sizeof(Infolist[0]);

int ReplaceStr(char *sSrc, char *sMatchStr, char *sReplaceStr)
{
    int  StringLen;
    char caNewString[MAX_MSG_LENGTH];
    char *FindPos = strstr(sSrc, sMatchStr);
    if( (!FindPos) || (!sMatchStr) )
        return -1;
    while( FindPos )        {
        memset(caNewString, 0, sizeof(caNewString));
        StringLen = FindPos - sSrc;
        strncpy(caNewString, sSrc, StringLen);
        strcat(caNewString, sReplaceStr);
        strcat(caNewString, FindPos + strlen(sMatchStr));
        strcpy(sSrc, caNewString);
        FindPos = strstr(sSrc, sMatchStr);
    }
    return 0;
}

int main(int argc, char *argv[])
{
    char postdata[MAX_MSG_LENGTH] = {0};
    char tmp[MAX_MSG_LENGTH]= {0};

    int i;
    int b64_len = 0;

    snprintf(postdata, sizeof(postdata) -1, "%s", "MiUwOTxTUkM6TVVTSUNfOC4wLjMuMV9CQ1M1fEFDVDpQTEFZX01VU0lDfFM6S3dNVnxVOjYzMDkwMTA3fE46fFQ6MHxOQTrLwNPjfEFSOr2vva98QUw6y8DT43xGOkhRfEI6MHxEOjB8UklEOk1VU0lDXzY2NjEyMTB8UzE6NDE5MTM0OTc3MnxTMjozNTI0MTA3NTIxfEZWOnxQRjowfFVJOnxEVDowfFBTUkM6RlJPTT3H+r/iLT4iy8DT4yK1xMvRy/e94bn7LT694bn7wdCx7SgxNjcyNSl8cmFkaW9fc3JjOjB8Tk9XUExBWTpzZWFyY2h8U0NSTjoxMzY2Kjc2OHxFTlBJOjAwMHxMU1JDOsSsyM/B0LHtfElTVklQOjB8VklQVFk6LTF8VklQTFY6LTF8RExZOjB8TVRSRUU6MHxHUzowfEZTQjowfFRNTTowfFRNUzowfE1VTFRJOjB8fEJMS1RNOjB8QkxLQ05UOjB8QlI6MHxGTVQ6d21hfEVORFRZUEU6MXxDVDp8Q0FDSEU6MXxDQUNIRURTWjozNjY4MjYzfENUWVBFOnNvbmcxfFBUOjIxNjF8RFVSOjIyN3xTUEVFRDowfEZTSVpFOjM2NjgyNjN8REVMQVk6NDZ8RkRNVDoxNXxGRE1QOjEwMHxGU1BUOjE1fEJMS1BPUzowfFNUQVJUU0laRToxNjM4NDB8QUFDUkVTVUxUOjB8Q0hBTk5FTF9OQU1FOlFEdHN0fFBST0Q6TVVTSUN8VkVSOjguMC4zLjFfQkNTNXxQTEFUOldJTjMyfEZST006a3dtdXNpYzIwMTUuZXhlfFVJOnx7a3dtdXNpYzIwMTUuZXhlfXxLOjE2ODA3NzAwfFJFU0VORDowfFU6NjMwOTAxMDc+AA==");
    for(i = 0; i < Infolistsize; i++) {
        b64_len = base64_decode(postdata, strlen(postdata), tmp);
        
        if(b64_len <= 0 || strncmp(tmp, "2%09<", 5) != 0 || tmp[b64_len-1] != '\x00') {
            fprintf(stderr, "info decode error %d %d [%c]\n", b64_len, strlen(tmp), tmp[b64_len-1] );
            fprintf(stderr, "[%s]\n", tmp);
            return -1;
        }
        //fprintf(stdout, "%s\n", tmp);
        if(i == 0) {
            char key[16] = {0};
            char uid[16] = {0};
            int j;
            char *p = NULL;

            p = strstr(tmp, "exe}|K:");
            if(p == NULL) {
                fprintf(stderr, "can't find key\n");
                return -1;
            }
            p += strlen("exe}|K:");
            j = 0;
            while(p && *p >= '0' && *p <='9'  ) {
                key[j++] = *p;
                p++;
            }

            printf("key: %s\n", key);
            p = strstr(tmp, "RESEND:0|U:");
            p += strlen("RESEND:0|U:");
            if(p == NULL) {
                fprintf(stderr, "can't find uid\n");
                return -1;
            }
            j = 0;
            while(p && *p >= '0' && *p <= '9' ) {
                uid[j++] = *p;
                p++;
            }
            printf("uid: %s\n", uid);

            if(ReplaceStr(tmp, uid, Infolist[0].uid) != 0 || ReplaceStr(tmp, key, Infolist[0].key) != 0) {
                fprintf(stderr, "ReplaceStr error\n");
                return -1;
            } else {
                //printf("%s\n", tmp);
                tmp[strlen(tmp)] = '\x00';
                b64_len = base64_encode(tmp, strlen(tmp)+1, postdata);
                fprintf(stdout, "b64_len: %d %s\n", b64_len, postdata);
            }
        } else {
            if(ReplaceStr(tmp, Infolist[i-1].uid, Infolist[i].uid) != 0 || ReplaceStr(tmp, Infolist[i-1].key, Infolist[i].key) != 0) {
                fprintf(stderr, "ReplaceStr error\n");
                return -1;
            } else {
                //printf("%s\n", tmp);
                tmp[strlen(tmp)] = '\x00';
                b64_len = base64_encode(tmp, strlen(tmp)+1, postdata);
                
            }
        }
        // POST INFO: postdata     
		fprintf(stdout, "%d: [%c] b64_len: %d \n%s\n", i, tmp[b64_len-1], b64_len, postdata);
    }
    return 0;

}